package member

import (
	"github.com/astaxie/beego/orm"
	"github.com/astaxie/beego/validation"
	"qycms/controllers"
	"qycms/models/member"
	"qycms/untils"
	"strings"
)
//会员分组
type MembersController struct {
    controllers.BaseController
}
//会员
type UserController struct {
	controllers.BaseController
}

/**
 * 会员页面
 */
func (this *UserController) Index()  {
	this.TplName = "members/user.html"
}
/**
 * 会员数据列表
 */
func (this *UserController) UserList(){
	//分页计算
	currentPage,err := this.GetInt("page",1)
	pagePerNum,_ := this.GetInt("limit",20)
	if(err != nil ){
		currentPage = 1
	}
	offsetNum := pagePerNum * (currentPage - 1)

	//数据查询
	var count int64 = 0
	member := []member.Member{}

	O := orm.NewOrm()
	q := O.QueryTable("sys_member_list")

	keyword := this.GetString("key")
	if keyword != ""{
		q = q.Filter("nickname__icontains",keyword)
	}

	count,_ = q.Filter("is_delete",0).Count()
	q.Filter("is_delete",0).Limit(pagePerNum).Offset(offsetNum).OrderBy("-id").RelatedSel().All(&member)

	this.Data["json"] = untils.ListJson(count,member)
	this.ServeJSON()
	this.StopRun()
}

/**
 * 新增会员页面
 */
func (this *UserController) AddMemberView(){
	o := orm.NewOrm()
	group := []member.Group{}
	o.QueryTable("sys_member_group").Filter("is_delete",0).All(&group)
	this.Data["place"] = group
	this.TplName = "members/AddMemberView.html"
}
/**
 * 新增会员
 */
func(this *UserController) AddMember() {
	//接收参数
	group_id,_ := this.GetInt("group_id")
	username:= this.GetString("username")
	pwd:= this.GetString("pwd")
	tel:= this.GetString("tel")
    sex,_ := this.GetInt("sex")
	open,_:= this.GetInt("open")
	score,_ := this.GetInt("score")
	coin,_ := this.GetInt("coin")

	//数据验证
	valid := validation.Validation{}
	valid.Required(group_id, "group_id").Message("请选择会员分组")
	valid.Required(username, "username").Message("用户名称不能为空")
	valid.Required(pwd, "pwd").Message("密码不能为空")
	valid.Required(tel, "tel").Message("电话不能为空")

	fileurl := this.GetString("pic")
	pic := this.Base64Upload(fileurl)

	//绑定到模型
	o := orm.NewOrm()
	group := member.Group{}
	o.QueryTable(new(member.Group)).Filter("id",group_id).One(&group)
	member := member.Member{
		UserName: username,
		Headpic: pic,
		Pwd: untils.GetMd5Str(pwd),
		NickName: this.GetString("nickname"),
		Sex:sex,
		Tel: this.GetString("tel"),
		Email: this.GetString("email"),
		Open: open,
		UserUrl: this.GetString("user_url"),
        Signature: this.GetString("signature"),
        Score: score,
        Coin: coin,
		Group:&group,
	}

	_, err := o.Insert(&member)

	if err != nil {
		this.Err(2001, "操作失败", nil)
	} else {
		this.Suc(nil)
	}
}

/**
 * 修改会员页面
 */
func (this *UserController) EditMemberView(){
	id,_ := this.GetInt("id")
	o := orm.NewOrm()

    //分组
	group := []member.Group{}
	o.QueryTable(new(member.Group)).Filter("is_delete",0).All(&group)

	member := member.Member{}
	o.QueryTable("sys_member_list").Filter("id",id).RelatedSel().One(&member);
	this.Data["place"] = group
	this.Data["member"] = member
	this.TplName = "members/EditMemberView.html"
}
/**
 * 新增会员
 */
func(this *UserController) UpdateMember() {
	//接收参数
	ID := this.GetString("Id")
	group_id,_ := this.GetInt("group_id")
	username:= this.GetString("username")
	pwd:= this.GetString("pwd")
	tel:= this.GetString("tel")
	sex,_ := this.GetInt("sex")
	open := this.GetString("open")
	score,_ := this.GetInt("score")
	coin,_ := this.GetInt("coin")

	//数据验证
	valid := validation.Validation{}
	valid.Required(group_id, "group_id").Message("请选择会员分组")
	valid.Required(username, "username").Message("用户名称不能为空")
	valid.Required(pwd, "pwd").Message("密码不能为空")
	valid.Required(tel, "tel").Message("电话不能为空")

	//绑定到模型
	var new_open int
	if open == "on"{
		new_open = 1
	}else{
		new_open = 0
	}

	fileurl := this.GetString("pic")
	var pic string
	if len(fileurl) == 0 {
		pic = this.GetString("oldpic")
	}else{
		pic = this.Base64Upload(fileurl)
	}

	//绑定到模型
	o := orm.NewOrm()

	member := orm.Params{
		"username": username,
		 "headpic": pic,
		"pwd": untils.GetMd5Str(pwd),
		"nickname": this.GetString("nickname"),
		"sex":sex,
		"tel": this.GetString("tel"),
		"email": this.GetString("email"),
		"open": new_open,
		"user_url": this.GetString("user_url"),
		"signature": this.GetString("signature"),
		"score": score,
		"coin": coin,
		"group_id":group_id,
	}

	_, err := o.QueryTable("sys_member_list").Filter("id",ID).Update(member)


	if err != nil {
		this.Err(2001, "操作失败", nil)
	} else {
		this.Suc(nil)
	}
}
/**
 * 删除会员
 */
func (this *UserController) DelMember(){
	//接收参数
	ids := this.GetString("ids");
	new_ids := ids[:len(ids)]
	id_arr := strings.Split(new_ids,",")

	//数据更新
	o := orm.NewOrm()
	qs := o.QueryTable(new(member.Member))
	for _,v :=range id_arr{
		id_int := untils.StrToInt(v)
		qs.Filter("id",id_int).Update(orm.Params{
			"is_delete":1,
		})
	}
	this.Suc(nil)
}


/**
 * 会员分组页面
 */
func (this *MembersController) Index()  {
	this.TplName = "members/index.html"
}
/**
 * 会员分组数据列表
 */
func (this *MembersController) Glist(){
	//分页计算
	currentPage,err := this.GetInt("page",1)
	pagePerNum,_ := this.GetInt("limit",20)
	if(err != nil ){
		currentPage = 1
	}
	offsetNum := pagePerNum * (currentPage - 1)

	//数据查询
	var count int64 = 0
	group := []member.Group{}

	O := orm.NewOrm()
	q := O.QueryTable("sys_member_group")

	keyword := this.GetString("key")
	if keyword != ""{
		q = q.Filter("mgroup_name__icontains",keyword)
	}

	count,_ = q.Filter("is_delete",0).Count()
	q.Filter("is_delete",0).Limit(pagePerNum).Offset(offsetNum).OrderBy("-id").RelatedSel().All(&group)

	this.Data["json"] = untils.ListJson(count,group)
	this.ServeJSON()
	this.StopRun()
}

/**
 * 新增会员分组
 */
func (this *MembersController) AddgView(){
	this.TplName = "members/AddgView.html"
}
/**
 * 新增会员分组
 */
func(this *MembersController) AddGroup() {
	//接收参数
	mgroup_name := this.GetString("mgroup_name")
	mgroup_toplimit,_ := this.GetInt("mgroup_toplimit")
	mgroup_bomlimit,_ := this.GetInt("mgroup_bomlimit")
	mgroup_open, _ := this.GetInt("mgroup_open")
	mgroup_order, _ := this.GetInt("mgroup_order")

	//数据验证
	valid := validation.Validation{}
	valid.Required(mgroup_name, "mgroup_name").Message("会员分组不能为空")
	valid.Required(mgroup_toplimit, "mgroup_toplimit").Message("积分上限不能为空")
	valid.Required(mgroup_bomlimit, "mgroup_bomlimit").Message("积分下限不能为空")
	valid.Required(mgroup_open, "mgroup_open").Message("请选择状态")
	valid.Required(mgroup_order, "mgroup_order").Message("请输入排序")

	//绑定到模型
	o := orm.NewOrm()
	group := member.Group{
		MgroupName:mgroup_name,
		MgroupToplimit:mgroup_toplimit,
		MgroupBomlimit: mgroup_bomlimit,
		MgroupOpen: mgroup_open,
		MgroupOrder: mgroup_order,
	}

	_, err := o.Insert(&group)

	if err != nil {
		this.Err(2001, "操作失败", nil)
	} else {
		this.Suc(nil)
	}
}
/**
 * 编辑分组
 */
func (this *MembersController) EditgView(){
	id,_ := this.GetInt("id")
	o := orm.NewOrm()

	group := member.Group{}
	o.QueryTable("sys_member_group").Filter("id",id).Filter("is_delete",0).One(&group)

	this.Data["group"] = group
	this.TplName = "members/EditgView.html"
}
/**
 * 更新会员分组
 */
func (this *MembersController) EditGroup(){
	//接收参数
	ID := this.GetString("Id")
	mgroup_name := this.GetString("mgroup_name")
	mgroup_toplimit,_ := this.GetInt("mgroup_toplimit")
	mgroup_bomlimit,_ := this.GetInt("mgroup_bomlimit")
	mgroup_open:= this.GetString("mgroup_open")
	mgroup_order, _ := this.GetInt("mgroup_order")

	//数据验证
	valid := validation.Validation{}
	valid.Required(mgroup_name, "mgroup_name").Message("会员分组不能为空")
	valid.Required(mgroup_toplimit, "mgroup_toplimit").Message("积分上限不能为空")
	valid.Required(mgroup_bomlimit, "mgroup_bomlimit").Message("积分下限不能为空")
	valid.Required(mgroup_open, "mgroup_open").Message("请选择状态")
	valid.Required(mgroup_order, "mgroup_order").Message("请输入排序")

	//绑定到模型
	o := orm.NewOrm()
	//绑定到模型
	var new_is_active int
	if mgroup_open == "on"{
		new_is_active = 1
	}else{
		new_is_active = 0
	}

	group := orm.Params{
		"mgroup_name": mgroup_name,
		"mgroup_open": new_is_active,
		"mgroup_toplimit": mgroup_toplimit,
		"mgroup_bomlimit": mgroup_bomlimit,
		"mgroup_order": mgroup_order,
	}

	_ ,err := o.QueryTable("sys_member_group").Filter("id",ID).Update(group)

	if err != nil {
		this.Err(2001,"操作失败",nil)
	}else{
		this.Suc(nil)
	}
}

//删除会员分组
func(this *MembersController) DelAction(){
	//接收参数
	ids := this.GetString("ids");
	new_ids := ids[:len(ids)]
	id_arr := strings.Split(new_ids,",")

	//数据更新
	o := orm.NewOrm()
	qs := o.QueryTable("sys_member_group")
	for _,v :=range id_arr{
		id_int := untils.StrToInt(v)
		qs.Filter("id",id_int).Update(orm.Params{
			"is_delete":1,
		})
	}
	this.Suc(nil)
}
